//
// Created by shao.liming on 2/5/2018.
//

#include <cstdlib>
#include "C103.h"


std::string C103::serialByPre(Node *head) {
    if (head == nullptr)
        return "#!";

    std::string str = std::to_string(head->value) + "!";
    str += serialByPre(head->left);
    str += serialByPre(head->right);

    return str;
}

Node *C103::reconByPreString(std::string preStr) {
    if (preStr.empty())
        return nullptr;

    auto n = (int) preStr.find('!');

    std::string valueStr = std::string(preStr.begin(), preStr.begin()+n);
//    printf("\n%s", valueStr.c_str());

    if(valueStr[0] == '#')
        return nullptr;

    Node *ret = (Node*)malloc(sizeof(Node)*1);

    int value = atoi(valueStr.c_str());
//    printf("\n%d", value);
    ret->value = value;

    ret->left = reconByPreString()
    preStr.erase(preStr.begin(), preStr.begin()+n+1);
    printf("\n%s", preStr.c_str());
    return NULL;
}


void C103::test() {
    /*
 *  使用如下二叉树
 *          1
 *        2    3
 *      4  5  6  7
 *     8        9  10
 *
 *  前序遍历：1 2 4 8 5 3 6 7 9 10
 *  中序遍历：8 4 2 5 1 6 3 9 7 10
 *  后序遍历：8 4 5 2 6 9 10 7 3 1
 */

    Node *head = newNode(1);
    head->left = newNode(2);
    head->right = newNode(3);
    head->left->left = newNode(4);
    head->left->right = newNode(5);
    head->right->left = newNode(6);
    head->right->right = newNode(7);
    head->left->left->left = newNode(8);
    head->right->right->left = newNode(9);
    head->right->right->right = newNode(10);

    /*
     * Pre: "1!2!4!8!#!#!#!5!#!#!3!6!#!#!7!9!#!#!10!#!#!"
     *
     *
     */

    std::string ret = serialByPre(head);
    printf("%s", ret.c_str());
    Node* pre = reconByPreString(ret);
}

Node *C103::newNode(int val) {
    auto *ret = (Node*)malloc(sizeof(Node));
    ret->value = val;
    ret->left = nullptr;
    ret->right = nullptr;
    return ret;
}



